package org.adorsys.aderp.aderplogin.security.hessian;

import javax.annotation.Resource;

import org.adorsys.aderp.aderplogin.client.common.Oauth2User;
import org.adorsys.aderp.aderplogin.client.common.Oauth2UserProvider;
import org.adorsys.aderp.aderplogin.client.oauth.Oauth2SystemUserService;
import org.adorsys.aderp.aderplogin.security.token.Oauth2TokenReplicationService;
import org.springframework.stereotype.Service;

/**
 * This runs only in the login application. Loads the user
 * directly from the service.
 * 
 * @author francis
 *
 */
@Service("oauth2UserProvider")
public class Oauth2UserProviderForLoginService implements Oauth2UserProvider {

	@Resource
	private Oauth2TokenReplicationService oauth2TokenReplicationService;
	
	@Resource
	private Oauth2SystemUserService oauth2SystemUserService;
	
	
	@Override
	public Oauth2User getUser(String token) {
		Oauth2User systemUser = oauth2SystemUserService.getSystemUser(token);
		if(systemUser!=null) return systemUser;
		return oauth2TokenReplicationService.loadUser(token, "aderp-login");
	}

}
